<template>
	<view class="page">

		<view class="p15">

			<view v-for="(item,index) in productList" :key="index" class="bg-white p12 u-flex brs5 mb10"
				style="align-items: flex-start;">
				<!-- <image :src="item.image" style="width:150rpx;height: 150rpx;border-radius: 5rpx;min-width:150rpx">
				</image> -->
				<view class="pl12 u-f1 pt2">
					<view class="u-flex u-f-jsb">
						<view class="fz16 u-font-bold u-line-2">{{item.name}}</view>

					</view>
					<view class="u-flex mt5">
						<view class="fz13 c6 u-line-2">{{item.content}}</view>
					</view>
					<view class="fz16 u-font-bold mt15 u-flex u-f-jsb" style="color: red;">
						￥{{item.price}}

						<view class="btn" @click="toOrder(item)">付款</view>
					</view>


				</view>
			</view>
		</view>



		<u-popup v-model="show" mode="bottom" border-radius="10" :closeable="true">
			<view style="padding: 80rpx 30rpx 100rpx;">

				<view class="bg-white p12 u-flex brs5 mb10" style="align-items: flex-start;">
					<!-- <image :src="curProduct.image"
						style="width:120rpx;height: 120rpx;border-radius: 5rpx;min-width:120rpx">
					</image> -->
					<view class="u-f1 pt2">
						<view class="u-flex u-f-jsb">
							<view class="fz16 u-font-bold u-line-2">{{curProduct.name}}</view>

						</view>
						<view class="fz16 u-font-bold mt15 u-flex u-f-jsb" style="color: red;">
							￥{{curProduct.price}}

						</view>


					</view>
				</view>
				<view class="u-flex">
					<view>姓名：</view>
					<u-input :custom-style="{'font-size':'32rpx'}" :clearable="false" :height="80" v-model="name"
						type="text" :border="true" placeholder="请输入姓名" />
				</view>
				<view class="mt15 u-flex" style="position: relative;">
					<view @click="showPicker2=true" style="position: absolute;z-index: 10;top:0;bottom:0;left:0;right:0">
					</view>
					<view>年级：</view>
					<u-input disabled :custom-style="{'font-size':'32rpx'}" :clearable="false" :height="80"
						v-model="gradeName" type="text" :border="true" placeholder="请选择年级" />
				</view>
				<view class="mt15 u-flex" style="position: relative;">
					<view @click="showPicker1=true" style="position: absolute;z-index: 10;top:0;bottom:0;left:0;right:0">
					</view>
					<view>班级：</view>
					<u-input disabled :custom-style="{'font-size':'32rpx'}" :clearable="false" :height="80"
						v-model="className" type="text" :border="true" placeholder="请选择班级" />
				</view>
				<template v-if="curProduct.category==1">
					
					<view class="mt15 u-flex">
						<view>宿舍：</view>
						<u-input :custom-style="{'font-size':'32rpx'}" :clearable="false" :height="80" v-model="bed"
							type="number" maxlength="3" :border="true" placeholder="请输入宿舍号，例：001" />
					</view>
					<view class="mt15 u-flex" style="position: relative;">
						<view @click="showPicker=true" style="position: absolute;z-index: 10;top:0;bottom:0;left:0;right:0">
						</view>
						<view>园区：</view>
						<u-input style="pointer-events：none" disabled :custom-style="{'font-size':'32rpx'}"
							:clearable="false" :height="80" v-model="garden" type="text" :border="true"
							placeholder="请选择园区" />
					</view>
				</template>


				<button style="margin: 50rpx;width: auto;" class="u-f-justify u-reset-button btn-nav"
					@click="toPay">确认支付</button>
			</view>
		</u-popup>
<u-picker @confirm="confirm2" mode="selector" v-model="showPicker2" :range="gradeList"></u-picker>
		<u-picker @confirm="confirm1" mode="selector" v-model="showPicker1" :range="classList"></u-picker>
		<u-picker @confirm="confirm" mode="selector" v-model="showPicker" :range="gardenList"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				showPicker: false,
				showPicker1: false,
				showPicker2: false,
				page: 1,
				gradeList: ['高一', '高二', '高三'],
				classList: [],
				gardenList: ['松园', '公寓A栋', '公寓B栋', '梅园', '兰园', '菊园', '竹园'],
				curProduct: {},
				info: {},
				name: '',
				className: '',
				gradeName:'',
				bed: '',
				garden: '',
				curCar: '',
				isDisabled: false,
				productList: [],
				curId: '',
				loadStatus: '',
			}
		},
		computed: {},
		onLoad(e) {
			// e.token = '441c0cf99da414a92147d9326e3f3dee'
			if (e.token) {
				this.$db.set('userToken', e.token);
				this.initData()
			} else {
				window.location.href = 'https://laundry.siyunxinxikeji.com/api/user/authorize'
			}
			
			for(let i=1;i<=30;i++){
				this.classList.push(i+'班')
			}
			this.classList.push('其他')
			console.log(this.classList,111)
		},
		onShareAppMessage() {},
		onShareTimeline() {},
		// 页面下拉到底部触发
		onReachBottom() {},
		methods: {
			confirm(e) {
				let id = e[0]
				this.garden = this.gardenList[id]
				this.showPicker = false
			},
			confirm1(e) {
				let id = e[0]
				this.className = this.classList[id]
				this.showPicker1 = false
			},
			confirm2(e) {
				let id = e[0]
				this.gradeName = this.gradeList[id]
				this.showPicker2 = false
			},
			toCall(phone) {



				uni.makePhoneCall({
					phoneNumber: phone
				})
			},
			toOrder(item) {
				if (!this.$db.get("userToken")) {


					this.$common.errorToShow('请先登录！')
					window.location.href = 'https://laundry.siyunxinxikeji.com/api/user/authorize'
					return
				}
				this.curProduct = item
				this.show = true
			},

			wxPay(pay_list) {

				// #ifdef H5
				let that = this
				console.log(pay_list, 1111)
				if (typeof WeixinJSBridge != "undefined") {
					return new Promise((resove, reject) => {
						WeixinJSBridge.invoke(
							'getBrandWCPayRequest', pay_list,
							function(res) {
								if (res.err_msg == "get_brand_wcpay_request:ok") {
									that.$common.errorToShow('支付成功!')

									that.show = false
									setTimeout(() => {
										uni.switchTab({
											url: '/pages/order/index'
										})
									}, 2500)
								} else {


									that.show = false
									that.$common.errorToShow('支付失败')
								}
							})
					});
				}
				// #endif
			},
			toPay() {

				if (!this.name) {
					this.$common.errorToShow('请输入姓名')
					return
				}
				// if (!this.className) {
				// 	this.$common.errorToShow('请输入班级')
				// 	return
				// }
				if(this.curProduct.category==1){
					
					if (!this.bed) {
						this.$common.errorToShow('请输入宿舍号')
						return
					}
					if (this.bed.length != 3) {
						this.$common.errorToShow('请输入正确的宿舍号')
						return
					}
				}
				if (this.isDisabled) {
					return
				}
				this.isDisabled = true

				let post = {
					name: this.name,
					url: 'https://laundry.siyunxinxikeji.com/',
					class: this.gradeName+this.className,
					bed: this.bed,
					garden: this.garden,
					good_id: this.curProduct.id,
				}
				this.$api.payment(post, res => {
					if (res.code) {

						this.isDisabled = false
						this.wxPay(res.data.pay)
					} else {


						this.isDisabled = false
						this.$common.errorToShow(res.msg)
					}
				})
			},
			toSelect() {
				uni.navigateTo({
					url: '/pages/user/car/list?type=select'
				})
			},
			initData() {
				this.$api.goodsList({
					page: 1,
					pagesize: 50,
				}, res => {
					if (res.code) {
						this.productList = res.data
					}
				})

			},
		}
	}
</script>

<style lang="scss">
	.btn {
		background: #4CA8E7;
		border-radius: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		color: #fff;
		padding: 14rpx 32rpx;
	}

	.btn-nav {
		background: #4CA8E7;
		border-radius: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		color: #fff;
		width: 100%;
		margin: 0 40rpx;
		height: 90rpx;
	}

	.tag {
		border-radius: 10rpx;
		border: 1px solid #ffd100;
		color: #ffd100;
		font-size: 24rpx;
		padding: 4rpx 20rpx;
		margin-right: 20rpx;
	}

	.btn1 {
		background: #4CA8E7;
		border-radius: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #fff;
		padding: 8rpx 40rpx;
	}
</style>